package cn.xdf.docenhance

import android.content.Context
import android.graphics.Bitmap
import android.util.Log

/**
 * 轻量增强器：复刻 DocScanUI 的增强参数与处理入口（不含 UI）。
 * 注：这里不直接依赖 ML Kit 内部 AIDL，实现一个可替换的策略接口，后续可接 Model/NDK。
 */
class Enhancer private constructor(
    private val context: Context,
    private val options: EnhanceOptions,
    private val engine: EnhanceEngine
) {
    data class EnhanceOptions(
        val enableTextEnhance: Boolean = true,      // 对应 tfj.b
        val textEnhanceLevel: Float = 0.4f,         // 对应 tfj.a [0..1]
        val enableLightness: Boolean = true,        // 对应 tfj.d
        val lightnessLevel: Float = 0.0f,           // 对应 tfj.c [-1..1]
        val presetFilter: Int? = null,              // 对应 tgd.a 可选滤镜 ID，不与基础增强同开
    )

    interface EnhanceEngine {
        fun enhance(input: Bitmap, options: EnhanceOptions): Bitmap
    }

    fun process(input: Bitmap): Bitmap {
        require(!(options.presetFilter != null && options.enableTextEnhance)) {
            "Basic enhancements and filter operation shouldn't be set together."
        }
        return try {
            engine.enhance(input, options)
        } catch (e: Exception) {
            Log.e(TAG, "Enhance failed", e)
            input
        }
    }

    class Builder(private val context: Context) {
        private var options = EnhanceOptions()
        private var engine: EnhanceEngine = MlkitDocumentEngine(context)

        fun setOptions(opts: EnhanceOptions) = apply { this.options = opts }
        fun setEngine(engine: EnhanceEngine) = apply { this.engine = engine }
        fun build() = Enhancer(context, options, engine)
    }

    private companion object { const val TAG = "Enhancer" }
}


